// Filename: 32TO64.CPP

/************   Picture converter to complex data  *****************\

1993 (c) CDI UI, The USSR Academy of Sciences, Samara
Two modes: (Real, Imag)     -> complex
           (Module,Phase)   -> complex

\*******************************************************************/

#include <io.h>
#include <dos.h>
#include <string.h>
#include "fft.h"
#include "indicat.h"

int _32to64( char *Src1Pic, char *Src2Pic, char *ComplexPic, int Mode ){
char *dob[] =  { "Real,Imag)}", "Module,Phase)}"};

if(Mode!=Real_Imag && Mode!=Mod_Arg) return InvalidParams;
int fsrc1 = open(Src1Pic,1), fsrc2 = open(Src2Pic,1);
if ( fsrc1 == -1 || fsrc2 == -1) return NoFile;
int head1[4],head2[4];
_read(fsrc1,head1,8); _read(fsrc2,head2,8);
if(head1[2]!=32 || head2[2]!=32) return WrongType;
int m=head1[0], n=head1[1];
if(head2[0]!=m || head2[1]!=n) return DifSizes;
if( (long)m*n*4+8+head1[3] != filelength(fsrc1) || 
    (long)m*n*4+8+head2[3] != filelength(fsrc2)   ) return InvalidPicture;
dfree freespace; getdfree(0,&freespace);

long fsp   = (long)freespace.df_avail*freespace.df_bsec*freespace.df_sclus;
long reqsp = (long)m*n*8+8+head1[3]+15;
if(fsp < reqsp) return NoDiskSpace;
float *sf1 = new float[n], *sf2 = new float[n];
Complex *str_dest = new Complex[n];
if ( !str_dest) return NoMemory;

head1[2]=64;
head1[3]=0;
int fdest = open(ComplexPic, 770, 128 );
_write(fdest,head1,8);

check_indicator Ind(m,"Real-Complex conversion");
int i;
if(Mode==Real_Imag){
for(i=0;i<m;i++) {
  _read(fsrc1, sf1, n*4);
  _read(fsrc2, sf2, n*4);
  for(int j=0;j<n;j++) { str_dest[j].re=sf1[j]; str_dest[j].im=sf2[j];
  }
  _write( fdest, str_dest, 8*n);
  Ind.update(i);
  if(Ind.check())
      { close(fsrc1); close(fsrc2); close(fdest); unlink(ComplexPic);
	delete str_dest; delete sf2; delete sf1; return User_break;
      }
}
}
else{
for(i=0;i<m;i++) {
  _read(fsrc1, sf1, n*4);
  _read(fsrc2, sf2, n*4);
  for(int j=0;j<n;j++) {
    str_dest[j].re=sf1[j]*cos(sf2[j]); str_dest[j].im=sf1[j]*sin(sf2[j]);
  }
  _write( fdest, str_dest, 8*n);
  Ind.update(i);
  if(Ind.check())
      { close(fsrc1); close(fsrc2); close(fdest); unlink(ComplexPic);
	delete str_dest; delete sf2; delete sf1; return User_break;
      }
}
}
delete sf1; delete sf2; delete str_dest;

char *comment = new char[head1[3] + head2[3] + 40];
  _read( fsrc1, comment, head1[3]);
  _read( fsrc2, comment + head1[3], head2[3]);
  close(fsrc1); close(fsrc2);
  comment[ head1[3] + head2[3] ] = 0;
  strcat( comment, "32TO64{(");
switch(Mode){
  case Real_Imag: strcat(comment,dob[0]); break;
  case Mod_Arg:   strcat(comment,dob[1]); break;
}
//int cl = strlen(comment);
//  lseek( fdest, 6, SEEK_SET ); _write( fdest, &cl, 2 );
//  lseek( fdest, (long)m*n*sizeof(Complex), SEEK_CUR );
//  _write( fdest, comment, cl );
  delete comment;
  close(fdest);
return 0;
}

